package com.example.app_translation.serviceiml;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.app_translation.servince.DealRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.Map;


@Service
public class DealRequestServiceImpl{
    @Value("${Api_SERVICE}")
    private  String api_service;
    @Value("${LOGIN}")
    private  String login;
    @Value("${CQUERY}")
    private  String cquery;
    @Value("${CQLQUERY}")
    private  String cqlquery;

   @Autowired
   DealRequestService dealRequestService;
  /**
   * @name: 创建表的方法
   * @params: @String sql
   * @description: 该方法直接通过DDL语句创建table在设置的数据库
   * @return: 返回是否成功
   * @exception/throws:
   * @author: zxb
   * @date:   2021/11/17
   */
    public boolean createTable(String sql) {
        return dealRequestService.createTable(sql);
    }

    /**
     * @name: 获取cloudcc的binding的方法
     * @params: @JSONObject json
     * @description: 该方法直接通过DDL语句创建table在设置的数据库
     * @return: 返回binding值供之后调用
     * @exception/throws:
     * @author: zxb
     * @date:   2021/11/17
     */
    public String getCode(JSONObject json){
        String url=json.getString("url");
        String user=json.getString("user");
        String password=json.getString("password");
        Map<String, Object> paramMap = new HashMap<String,Object>();
        paramMap.put("serviceName",login);
        paramMap.put("password",password);
        paramMap.put("userName",user);
        String s = HttpUtil.get(url+api_service, paramMap);
        JSONObject jsonObject = JSONObject.parseObject(s);
        String returnCode = jsonObject.getString("returnCode");
        System.out.println("returnCode = " + returnCode);
        return returnCode;
    }

    /**
     * @name: 获取cloudcc的binding的方法
     * @params: @JSONObject json
     * @description: 该方法直接通过DDL语句创建table在设置的数据库
     * @return: 返回binding值供之后调用
     * @exception/throws:
     * @author: zxb
     * @date:   2021/11/17
     */
    public String getBindings(JSONObject json){
        String url=json.getString("url");
        String user=json.getString("user");
        String password=json.getString("password");
        Map<String, Object> paramMap = new HashMap<String,Object>();
        paramMap.put("serviceName",login);
        paramMap.put("password",password);
        paramMap.put("userName",user);
        String s = HttpUtil.get(url+api_service, paramMap);
        JSONObject jsonObject = JSONObject.parseObject(s);
        String binding = jsonObject.getString("binding");
        return binding;
    }
    /**
     * @name: 直接获取cloudcc某个api对象的数据
     * @params: @JSONObject json
     * @description: 该方法直接通过调用标准的Cloudcc的接口进行实现
     * @return: 返回目前是返回data的jsonarr字符串
     * @exception/throws:
     * @author: zxb
     * @date:   2021/11/17
     */
    public String getDataByApi(JSONObject json){
        String url=json.getString("url");
        String binding=json.getString("binding");
        String objectApiName=json.getString("api");
        Map<String, Object> paramMap = new HashMap<String,Object>();
        paramMap.put("serviceName",cquery);
        paramMap.put("binding",binding);
        paramMap.put("objectApiName",objectApiName);
        paramMap.put("isAddDelete",false);
        paramMap.put("expressions","is_deleted='0'");
        String s = HttpUtil.get(url+api_service, paramMap);
        JSONObject result = JSONObject.parseObject(s);
        JSONArray data = result.getJSONArray("data");
        return data.toJSONString();
    }

    /**
     * @name: 直接获取cloudcc某个api对象未删的数据的数量
     * @params: @JSONObject json
     * @description: 该方法直接通过调用标准的Cloudcc的接口进行实现
     * @return: 返回目前是返回data的size
     * @exception/throws:
     * @author: zxb
     * @date:   2021/11/17
     */
    public Integer getSizeBySql(JSONObject json){
        String url=json.getString("url");
        String binding=json.getString("binding");
        String objectApiName=json.getString("api");
        String sql_size="select count(id) as num from "+objectApiName+" where is_deleted='0'";
        Map<String, Object> paramMap = new HashMap<String,Object>();
        paramMap.put("serviceName",cqlquery);
        paramMap.put("binding",binding);
        paramMap.put("objectApiName",objectApiName);
        paramMap.put("expressions",sql_size);
        String s = HttpUtil.get(url+api_service, paramMap);
        JSONObject result = JSONObject.parseObject(s);
        JSONArray data = result.getJSONArray("data");
        String num = data.getJSONObject(0).getString("num");
        return Integer.parseInt(num);
    }

    public JSONArray getWordsBySql(JSONObject json){
        String url=json.getString("url");
        String binding=json.getString("binding");
        String objectApiName=json.getString("api");
        String sql_size="SELECT " +
                " s.id,m.LABEL_NAME,APINAME " +
                "FROM " +
                " tp_sys_schemetable s," +
                " tp_sys_object o," +
                " tp_sys_multi_lang m" +
                " WHERE " +
                " s.SCHEMETABLE_ID = o.id " +
                "AND s.id = m.RELATION_ID " +
                "AND m.type = 'field' " +
                "AND o.SCHEMETABLE_NAME = '"+objectApiName+"' " +
                "and m.COUNTRY='zh' ";
        Map<String, Object> paramMap = new HashMap<String,Object>();
        paramMap.put("serviceName",cqlquery);
        paramMap.put("binding",binding);
        paramMap.put("objectApiName",objectApiName);
        paramMap.put("expressions",sql_size);
        String s = HttpUtil.get(url+api_service, paramMap);
        JSONObject result = JSONObject.parseObject(s);
        JSONArray data = result.getJSONArray("data");
        return  data;
    }
    /**
     * @name: 直接获取cloudcc某个api对象的数据
     * @params: @JSONObject json
     * @description: 该方法直接通过调用标准的Cloudcc的接口进行实现
     * @return: 返回目前是返回data的jsonarr字符串
     * @exception/throws:
     * @author: zxb
     * @date:   2021/11/17
     */
    public   JSONArray getDataBySql(JSONObject json){
        String url=json.getString("url");
        String binding=json.getString("binding");
        String objectApiName=json.getString("api");
        String sql=json.getString("sql");
        System.out.println(Thread.currentThread().getName()+"sql = " + sql);
        Map<String, Object> paramMap = new HashMap<String,Object>();
        paramMap.put("serviceName",cqlquery);
        paramMap.put("binding",binding);
        paramMap.put("objectApiName",objectApiName);
        paramMap.put("expressions",sql);
        String s = HttpUtil.get(url+api_service, paramMap);
        JSONObject result = JSONObject.parseObject(s);
        JSONArray data = result.getJSONArray("data");
        System.out.println("size = " + data.size());
        return  data;
    }
    /**
     * @name:
     * @params:
     * @description:在连接的数据库创建表信息
     * @return:
     * @exception/throws:
     * @author: zxb
     * @date:   2021/12/8
     */
    public boolean creatTable(String sql){
        return  dealRequestService.createTable(sql);
    }
    public boolean insertData(String sql){
        return  dealRequestService.insertRow(sql);
    }

}
